package com.xbw.bookSale.biz;

import java.sql.SQLException;
import java.util.*;

import com.xbw.bookSale.dao.*;
import com.xbw.bookSale.po.Book;

public class BookBiz {

	BookDAO dao = new BookDAO();

	public List<Book> getAllBooks() {
		try {
			dao.setConn(BaseDAO.getConnection());
			List<Book> bookList = dao.getAllBooks();
			dao.getClose();
			return bookList;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Book>();
		}
	}

	public List<Book> getByBookNameAndTypeId(String bookName, int typeId) {
		try {
			dao.setConn(BaseDAO.getConnection());
			List<Book> bookList = dao.getByBookNameAndTypeId(bookName, typeId);
			dao.getClose();
			return bookList;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Book>();
		}
	}

	public boolean deleteBookById(int id) {
		try {
			dao.setConn(BaseDAO.getConnection());
			boolean isDeleteOk = dao.deleteBookById(id);
			dao.getClose();
			return isDeleteOk;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean addBook(Book book) {
		try {
			dao.setConn(BaseDAO.getConnection());
			boolean isAddOk = dao.addBook(book);
			dao.getClose();
			return isAddOk;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Book getBookByID(int id) {
		try {
			dao.setConn(BaseDAO.getConnection());
			Book book = dao.getBookByID(id);
			dao.getClose();
			return book;
		} catch (SQLException e) {
			e.printStackTrace();
			return new Book();
		}
	}
}
